package com.ruoyi.mateTemplate.service.impl;

import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.mateCheck.domain.MateCheck;
import com.ruoyi.mateCheck.domain.MateCheckModel;
import com.ruoyi.mateCheck.domain.MateCheckProduct;
import com.ruoyi.mateCheck.domain.MateCheckSample;
import com.ruoyi.mateCheck.mapper.MateCheckMapper;
import com.ruoyi.mateInventory.domain.MateInventory;
import com.ruoyi.mateInventory.domain.MateInventoryModel;
import com.ruoyi.mateInventory.domain.MateInventoryPlan;
import com.ruoyi.mateInventory.domain.MateInventoryPlanModel;
import com.ruoyi.mateInventory.mapper.MateInventoryMapper;
import com.ruoyi.mateTemplate.domain.MpTemplate;
import com.ruoyi.mateTemplate.domain.MpTemplateModel;
import com.ruoyi.mateTemplate.service.MpTemplateService;
import com.ruoyi.mateWarehousing.domain.MateWarehousing;
import com.ruoyi.mateWarehousing.mapper.MateWarehousingMapper;
import com.ruoyi.material.domain.MateEvaluate;
import com.ruoyi.material.domain.MateImport;
import com.ruoyi.material.domain.MateImportModel;
import com.ruoyi.material.domain.MateProductDetail;
import com.ruoyi.material.domain.MateProductDetailModel;
import com.ruoyi.material.domain.MateProductDevice;
import com.ruoyi.material.domain.MateQualified;
import com.ruoyi.material.domain.MateQualifiedModel;
import com.ruoyi.material.mapper.CheckReportMapper;
import com.ruoyi.material.mapper.DetailMapper;
import com.ruoyi.material.mapper.MateImportMapper;
import com.ruoyi.material.mapper.MateManufactuterMapper;
import com.ruoyi.material.mapper.MateProductMapper;
import com.ruoyi.material.mapper.MateQualifiedMapper;
import com.ruoyi.mtAudit.domain.MtAudit;
import com.ruoyi.mtAudit.mapper.MtAuditMapper;
import com.ruoyi.warehousing.domain.WarehousingRecords;
import com.ruoyi.warehousing.mapper.WarehousingRecordsMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @Author
 * @Version 1.0
 */
@Service
public class MpTemplateServiceImpl implements MpTemplateService {

    @Autowired
    private MateInventoryMapper mateInventoryMapper;

    @Autowired
    private MateManufactuterMapper mateManufactuterMapper;

    @Autowired
    private MateQualifiedMapper mateQualifiedMapper;

    @Autowired
    private MateCheckMapper mateCheckMapper;

    @Autowired
    private MateProductMapper mateProductMapper;

    @Autowired
    private CheckReportMapper checkReportMapper;

    @Autowired
    private MtAuditMapper mtAuditMapper;

    @Autowired
    private DetailMapper detailMapper;

    @Autowired
    private MateImportMapper mateImportMapper;

    @Autowired
    private MateWarehousingMapper mateWarehousingMapper;
    @Autowired
    private WarehousingRecordsMapper warehousingRecordsMapper;


    public static void main(String[] args) {
        String a = "32";
        for (int i = 0; i < a.length(); i++) {
            System.out.println(i);
        }
    }

    @Override
    public int addTemplate(MpTemplate mpTemplate) {
        int num = 0;
        try {
            List<MpTemplateModel> mpTemplateModels = mpTemplate.getMpTemplateModels();
            Long deptId = SecurityUtils.getDeptId();
            Map<String, Object> resMap = new HashMap<>();
            Date now2 = Calendar.getInstance().getTime();
            MateInventory mateInventory = new MateInventory();
            mateInventory.setDeptId(deptId);
            mateInventory.setAuditId(mpTemplate.getAuditId());
            mateInventory.setAuditName(mpTemplate.getAuditName());
            mateInventory.setInventoryNum(mpTemplate.getInventoryNum());
            mateInventory.setInventoryStatus("1");
            mateInventoryMapper.addMateInventory(mateInventory);

            MateInventoryModel mateInventoryModel = new MateInventoryModel();
            for (MpTemplateModel mpTemplateModel : mpTemplateModels) {
                resMap.put("deptId", deptId);
                resMap.put("mateProductId", mpTemplateModel.getMateproductId());
                mateInventoryModel.setDeptId(deptId);
                mateInventoryModel.setInventoryId(mateInventory.getInventoryId());
                mateInventoryModel.setMateproductId(mpTemplateModel.getMateproductId());
                mateInventoryModel.setMateProductName(mateProductMapper.selectMateProductName(resMap));
                mateInventoryModel.setNum(Long.valueOf(mpTemplateModel.getBuyNum()));
                mateInventoryModel.setMateProductType(mpTemplateModel.getMateProductType());
                mateInventoryModel.setMateCategory(mpTemplateModel.getMateCategory());
                mateInventoryModel.setExpectTime(mpTemplateModel.getExpectTime());
                mateInventoryModel.setMaterialId(mpTemplateModel.getMaterialId());
                mateInventoryModel.setMaterialName(mateManufactuterMapper.selectMaterialName(mpTemplateModel.getMaterialId()));
                mateInventoryModel.setMateStoredepict(mpTemplateModel.getMateStoredepict());
                mateInventoryMapper.insertMateInventoryModel(mateInventoryModel);
            }

            MtAudit mtAudit = new MtAudit();
            mtAudit.setCompanyId(SecurityUtils.getDeptId());
            mtAudit.setOrderId(mateInventory.getInventoryId());
            mtAudit.setZjId(mateInventory.getAuditId());
            mtAudit.setZjName(mateInventory.getAuditName());
            mtAudit.setOrderType("2");
            mtAudit.setOrderNum(mateInventory.getInventoryNum());
            mtAudit.setAuditTime(now2);
            mtAudit.setAuditStatus("1");
            mtAudit.setAuditBy(SecurityUtils.getLoginUser().getNickName());
            mtAuditMapper.insertMtAudit(mtAudit);

            MateInventoryPlan mateInventoryPlan = new MateInventoryPlan();
            MateInventoryPlanModel mateInventoryPlanModel = new MateInventoryPlanModel();
            mateInventoryPlan.setQzTime(mpTemplate.getCheckPlanTime());
            mateInventoryPlan.setDeptId(deptId);
            mateInventoryPlan.setInventoryPlanNum(mpTemplate.getInventoryPlanNum());
            mateInventoryPlan.setAuditId(mpTemplate.getAuditId());
            mateInventoryPlan.setAuditName(mpTemplate.getAuditName());
            mateInventoryPlan.setInventoryPlanStatus("1");
            mateInventoryMapper.addMateInventoryPlan(mateInventoryPlan);

            for (MpTemplateModel mpTemplateModel : mpTemplateModels) {
                resMap.put("deptId", deptId);
                resMap.put("mateProductId", mpTemplateModel.getMateproductId());
                mateInventoryPlanModel.setMateproductId(mpTemplateModel.getMateproductId());
                mateInventoryPlanModel.setExpectTime(mpTemplateModel.getExpectTime());
                mateInventoryPlanModel.setBuyNum(mpTemplateModel.getBuyNum());
                mateInventoryPlanModel.setDeptId(deptId);
                mateInventoryPlanModel.setMateProductType(mpTemplateModel.getMateProductType());
                mateInventoryPlanModel.setInventoryPlanId(mateInventoryPlan.getInventoryPlanId());
                mateInventoryPlanModel.setMateproductId(mpTemplateModel.getMateproductId());
                mateInventoryPlanModel.setMateProductName(mateProductMapper.selectMateProductName(resMap));
                mateInventoryPlanModel.setMateCategory(mpTemplateModel.getMateCategory());
                mateInventoryPlanModel.setMaterialId(mpTemplateModel.getMaterialId());
                mateInventoryPlanModel.setMaterialName(mateManufactuterMapper.selectMaterialName(mpTemplateModel.getMaterialId()));
                mateInventoryPlanModel.setMateStoredepict(mpTemplateModel.getMateStoredepict());
                mateInventoryMapper.insertMateInventoryPlanModel(mateInventoryPlanModel);
            }

            MtAudit mtAudit1 = new MtAudit();
            mtAudit1.setCompanyId(SecurityUtils.getDeptId());
            mtAudit1.setOrderId(mateInventoryPlan.getInventoryPlanId());
            mtAudit1.setZjId(mateInventoryPlan.getAuditId());
            mtAudit1.setZjName(mateInventoryPlan.getAuditName());
            mtAudit1.setOrderType("3");

            mtAudit1.setOrderNum(mateInventoryPlan.getInventoryPlanNum());
            mtAudit1.setAuditTime(now2);
            mtAudit1.setAuditStatus("1");
            mtAudit1.setAuditBy(SecurityUtils.getLoginUser().getNickName());
            mtAuditMapper.insertMtAudit(mtAudit1);
            MateEvaluate mateEvaluate = new MateEvaluate();
            for (MpTemplateModel mpTemplateModel : mpTemplateModels) {
                MateEvaluate mateEvaluate1 = mateManufactuterMapper.selectEvaluate(mpTemplateModel.getMateproductId());
                if (mateEvaluate1 == null) {
                    Map<String, Object> evaluateMap = new HashMap<>();
                    evaluateMap.put("deptId", deptId);
                    evaluateMap.put("mateProductId", mpTemplateModel.getMateproductId());
                    mateEvaluate.setDeptId(SecurityUtils.getDeptId());
                    mateEvaluate.setMateproductId(mpTemplateModel.getMateproductId());
                    mateEvaluate.setMaterialName(mateManufactuterMapper.selectMaterialName(mpTemplateModel.getMaterialId()));
                    mateEvaluate.setMaterialId(mpTemplateModel.getMaterialId());
                    mateEvaluate.setMfAbility(mpTemplateModel.getMfAbility());
                    mateEvaluate.setMfPeriod(mpTemplateModel.getMfPeriod());
                    mateEvaluate.setMfTrait("成批");
                    mateEvaluate.setMfFaclity("齐全、良好");
                    mateEvaluate.setMfInspection("检验设备良好");
                    mateEvaluate.setIsAttestation("是");
                    mateEvaluate.setMateProductName(mateProductMapper.selectMateProductName(evaluateMap));
                    mateEvaluate.setScAdvice("可以纳入");
                    mateEvaluate.setQuAdvice("可以纳入");
                    mateEvaluate.setMaAdvice("可以纳入合格供方名录");
                    mateEvaluate.setPuAdvice("可以纳入");
                    mateEvaluate.setPhone(mpTemplateModel.getMaterialPhone());
                    mateEvaluate.setApplyId(mpTemplate.getApplyId());
                    mateEvaluate.setApplyName(mpTemplate.getApplyName());
                    mateEvaluate.setEvaluateOrderNum(mpTemplate.getEvaluateOrderNum());
                    mateEvaluate.setQzTime(mpTemplate.getEvaluateTime());
                    mateEvaluate.setMateAddress(mpTemplateModel.getMateAddress());
                    mateManufactuterMapper.insertEvaluate(mateEvaluate);

                    MtAudit mtAudit2 = new MtAudit();
                    mtAudit2.setCompanyId(SecurityUtils.getDeptId());
                    mtAudit2.setOrderId(mateEvaluate.getEvaluateId());
                    mtAudit2.setZjId(mpTemplate.getApplyId());
                    mtAudit2.setZjId(mpTemplate.getApplyId());
                    mtAudit2.setZjName(mpTemplate.getApplyName());
                    mtAudit2.setOrderType("18");
                    mtAudit2.setOrderNum(mateEvaluate.getEvaluateOrderNum());
                    mtAudit2.setAuditTime(now2);
                    mtAudit2.setAuditStatus("1");
                    mtAudit2.setAuditBy(SecurityUtils.getLoginUser().getNickName());
                    mtAuditMapper.insertMtAudit(mtAudit2);
                }
            }

            MateQualified mateQualified = new MateQualified();
            MateQualifiedModel mateQualifiedModel = new MateQualifiedModel();
            mateQualified.setDeptId(deptId);
            mateQualified.setBzId(mpTemplate.getQualifiedBzId());
            mateQualified.setBzName(mpTemplate.getQualifiedBzName());
            mateQualified.setPzId(mpTemplate.getQualifiedPzId());
            mateQualified.setPzName(mpTemplate.getQualifiedPzName());
            mateQualified.setCheckDate(mpTemplate.getQualifiedDate());
            mateQualified.setQualifiedOrderNum(mpTemplate.getQualifiedOrderNum());
            mateQualifiedMapper.addMateQualified(mateQualified);

            for (MpTemplateModel mpTemplateModel : mpTemplateModels) {
                resMap.put("deptId", deptId);
                resMap.put("mateProductId", mpTemplateModel.getMateproductId());
                mateQualifiedModel.setDeptId(deptId);
                mateQualifiedModel.setQualifiedId(mateQualified.getQualifiedId());
                mateQualifiedModel.setMaterialId(mpTemplateModel.getMaterialId());
                mateQualifiedModel.setMateProductName(mateProductMapper.selectMateProductName(resMap));
                mateQualifiedModel.setMaterialName(mateManufactuterMapper.selectMaterialName(mpTemplateModel.getMaterialId()));
                mateQualifiedModel.setRemark(mpTemplateModel.getRemark());
                mateQualifiedModel.setPhone(mpTemplateModel.getMaterialPhone());
                mateQualifiedModel.setMateAddress(mpTemplateModel.getMateAddress());
                mateQualifiedMapper.addMateQualifiedModel(mateQualifiedModel);
            }

            MtAudit mtAudit3 = new MtAudit();
            mtAudit3.setCompanyId(deptId);
            mtAudit3.setOrderId(mateQualified.getQualifiedId());
            mtAudit3.setZjId(mateQualified.getPzId());
            mtAudit3.setZjName(mateQualified.getPzName());
            mtAudit3.setOrderType("19");
            mtAudit3.setOrderNum(mateQualified.getQualifiedOrderNum());
            mtAudit3.setAuditTime(now2);
            mtAudit3.setAuditStatus("1");
            mtAudit3.setAuditBy(SecurityUtils.getLoginUser().getNickName());
            mtAuditMapper.insertMtAudit(mtAudit3);
            MateProductDetail mateProductDetail = new MateProductDetail();
            mateProductDetail.setDeptId(deptId);
            mateProductDetail.setDetailOrderNum(mpTemplate.getDetailOrderNum());
            detailMapper.insertDetail(mateProductDetail);

            for (MpTemplateModel mpTemplateModel : mpTemplateModels) {
                resMap.put("deptId", deptId);
                resMap.put("mateProductId", mpTemplateModel.getMateproductId());
                MateProductDetailModel mateProductDetailModel = new MateProductDetailModel();
                mateProductDetailModel.setDetailId(mateProductDetail.getDetailId());
                mateProductDetailModel.setDeptId(deptId);
                mateProductDetailModel.setMateproductId(mpTemplateModel.getMateproductId());
                mateProductDetailModel.setMateProductName(mateProductMapper.selectMateProductName(resMap));
                mateProductDetailModel.setMaterialId(mpTemplateModel.getMaterialId());
                mateProductDetailModel.setMaterialName(mateManufactuterMapper.selectMaterialName(mpTemplateModel.getMaterialId()));
                mateProductDetailModel.setMateCategory(mpTemplateModel.getMateCategory());
                mateProductDetailModel.setSupplyAbility(mpTemplateModel.getMfAbility());
                detailMapper.insertDetailModel(mateProductDetailModel);

            }

            MtAudit mtAudit6 = new MtAudit();
            mtAudit6.setCompanyId(deptId);
            mtAudit6.setOrderId(mateProductDetail.getDetailId());
            mtAudit6.setZjId(mpTemplate.getZjId());
            mtAudit6.setZjName(mpTemplate.getZjName());
            mtAudit6.setOrderType("20");
            mtAudit6.setOrderNum(mateProductDetail.getDetailOrderNum());
            mtAudit6.setAuditTime(now2);
            mtAudit6.setAuditStatus("1");
            mtAudit6.setAuditBy(SecurityUtils.getLoginUser().getNickName());
            mtAuditMapper.insertMtAudit(mtAudit6);
            String reportOrderNum = "";

            for (MpTemplateModel mpTemplateModel : mpTemplateModels) {
                resMap.put("deptId", deptId);
                resMap.put("mateProductId", mpTemplateModel.getMateproductId());
                MateCheck mateCheck = new MateCheck();
                mateCheck.setDeptId(deptId);
                mateCheck.setCheckStatus("1");
                mateCheck.setCheckResult("合格");
                mateCheck.setCheckOrdernum(mpTemplate.getCheckOrderNum());
                mateCheck.setIsPackage("完好");
                mateCheck.setPurchaseInventory("有");
                mateCheck.setQualified("是");
                mateCheck.setTypeAgree("相符");
                mateCheck.setInvoice("有");
                mateCheck.setQualifiedProve("有");
                mateCheck.setEnchase("有");
                mateCheck.setRealityNum("相符");
                mateCheck.setAuditTime(mpTemplate.getCheckAuditTime());
                mateCheck.setInformationList(mpTemplate.getInformationList().toString());
                mateCheck.setAuditId(mpTemplate.getCheckAuditId());
                mateCheck.setAuditName(mpTemplate.getCheckAuditName());
                mateCheck.setZjId(mpTemplate.getZjId());
                mateCheck.setZjName(mpTemplate.getZjName());
                mateCheck.setCheckTime(mpTemplate.getCheckTime());
                mateCheckMapper.addCheckInformation(mateCheck);
                MtAudit mtAudit4 = new MtAudit();
                mtAudit4.setCompanyId(deptId);
                mtAudit4.setOrderId(mateCheck.getCkInformationId());
                mtAudit4.setZjId(mateCheck.getAuditId());
                mtAudit4.setZjName(mateCheck.getAuditName());
                mtAudit4.setOrderType("0");
                mtAudit4.setOrderNum(mateCheck.getCheckOrdernum());
                mtAudit4.setAuditTime(now2);
                mtAudit4.setAuditStatus("1");
                mtAudit4.setAuditBy(SecurityUtils.getLoginUser().getNickName());
                mtAuditMapper.insertMtAudit(mtAudit4);

                MateCheckProduct mateCheckProduct = new MateCheckProduct();
                mateCheckProduct.setDeptId(deptId);
                mateCheckProduct.setMateproductId(mpTemplateModel.getMateproductId());
                mateCheckProduct.setMateProductName(mateProductMapper.selectMateProductName(resMap));
                mateCheckProduct.setMateProductType(mpTemplateModel.getMateProductType());
                mateCheckProduct.setAllNum(mpTemplateModel.getBuyNum());
                mateCheckProduct.setMaterialId(mpTemplateModel.getMaterialId());
                mateCheckProduct.setEnterNum(mpTemplateModel.getEnterNum());
                mateCheckProduct.setMateBatch(mpTemplateModel.getMateBatch());
                mateCheckProduct.setPurchaseTime(mpTemplateModel.getExpectTime());
                mateCheckProduct.setCkInformationId(mateCheck.getCkInformationId());
                mateCheckMapper.insertCheckProduct(mateCheckProduct);

                List<MateProductDevice> mateProductDevices = mateProductMapper.selectMateProductDevice(mpTemplateModel.getMateproductId());
                for (MateProductDevice mateProductDevice : mateProductDevices) {
                    MateCheckModel mateCheckModel = new MateCheckModel();
                    mateCheckModel.setDeptId(deptId);
                    mateCheckModel.setCkInformationId(mateCheck.getCkInformationId());
                    mateCheckModel.setCheckCriterion(mateProductDevice.getCheckCriterion());
                    mateCheckModel.setCheckProject(mateProductDevice.getCheckProject());
                    mateCheckModel.setCheckNum(mpTemplateModel.getCheckNum().toString());
                    mateCheckModel.setIsNum(mateProductDevice.getIsNum());
                    mateCheckModel.setMinNum(mateProductDevice.getMinNum());
                    mateCheckModel.setMaxNum(mateProductDevice.getMaxNum());
                    mateCheckModel.setCheckUnit(mateProductDevice.getUnit());
                    mateCheckModel.setCheckFlow(mateProductDevice.getCheckFlow());
                    mateCheckModel.setCheckResult(mateProductDevice.getCheckResult());
                    mateCheckMapper.insertCheckModel(mateCheckModel);
                }

                reportOrderNum = mateCheck.getCheckOrdernum();
                List<MateCheckModel> mateCheckModels = mateCheckMapper.selectModels(mateCheck.getCkInformationId());
                for (MateCheckModel mateCheckModel : mateCheckModels) {
                    for (int i = 0; i < Integer.valueOf(mateCheckModel.getCheckNum()); i++) {
                        int nums = i + 1;
                        MateCheckSample mateCheckSample = new MateCheckSample();
                        if (mateCheckModel.getIsNum() == 1) {
                            mateCheckSample.setDeptId(deptId);
                            mateCheckSample.setSampleName("样本" + nums);
                            mateCheckSample.setCheckNo(mateCheckModel.getCheckNo());
                            mateCheckSample.setSampleResult(mateCheckModel.getCheckResult());
                            mateCheckSample.setCkInformationId(mateCheckModel.getCkInformationId());
                            mateCheckMapper.insertCheckSample(mateCheckSample);
                        } else {
                            mateCheckSample.setDeptId(deptId);
                            mateCheckSample.setSampleName("样本" + nums);
                            mateCheckSample.setCheckNo(mateCheckModel.getCheckNo());
                            mateCheckSample.setCkInformationId(mateCheckModel.getCkInformationId());
                            Double minValue = mateCheckModel.getMinNum();
                            Double maxValue = mateCheckModel.getMaxNum();
                            double randomValue = minValue + (maxValue - minValue) * new Random().nextDouble();
                            // 保留两位小数
                            DecimalFormat df = new DecimalFormat("#.##");
                            String formattedResult = df.format(randomValue);
                            mateCheckSample.setSampleResult(formattedResult);
                            mateCheckMapper.insertCheckSample(mateCheckSample);
                        }

                    }
                }
                SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS Z");
              /*  MateProductCheck mateProductCheck = new MateProductCheck();
                mateProductCheck.setDeptId(deptId);
                mateProductCheck.setZjName(mpTemplate.getZjName());
                mateProductCheck.setAuditName(mpTemplate.getCheckAuditName());
                String time = mpTemplate.getCheckAuditTime().replace("Z", " UTC");
                mateProductCheck.setAuditTime(format.parse(time));
                mateProductCheck.setCheckResult(mpTemplate.getReportResult());
                mateProductCheck.setCheckTime(mpTemplate.getCheckTime());
                mateProductCheck.setMpcheckStatus("1");
                mateProductCheck.setOrderNum(mpTemplate.getReportOrderNum());
                mateProductCheck.setIsPackage("完好");
                mateProductCheck.setPurchaseInventory("有");
                mateProductCheck.setQualified("是");
                mateProductCheck.setTypeAgree("相符");
                mateProductCheck.setInvoice("有");
                mateProductCheck.setQualifiedProve("有");
                mateProductCheck.setEnchase("有");
                mateProductCheck.setRealityNum("相符");
                mateProductCheck.setInformationList("无");
                checkReportMapper.addCheck(mateProductCheck);

                MtAudit mtAudit5 = new MtAudit();
                mtAudit5.setCompanyId(deptId);
                mtAudit5.setOrderId(mateProductCheck.getMateCheckId());
                mtAudit5.setZjName(mateProductCheck.getAuditName());
                mtAudit5.setOrderType("1");
                mtAudit5.setOrderNum(mateProductCheck.getOrderNum());
                mtAudit5.setAuditTime(now2);
                mtAudit5.setAuditStatus("1");
                mtAudit5.setAuditBy(SecurityUtils.getLoginUser().getNickName());
                mtAuditMapper.insertMtAudit(mtAudit5);

                MateProductReport mateProductReport = new MateProductReport();
                mateProductReport.setMateCheckId(mateProductCheck.getMateCheckId());
                mateProductReport.setDeptId(deptId);
                mateProductReport.setMateproductId(mpTemplateModel.getMateproductId());
                mateProductReport.setMateProductType(mpTemplateModel.getMateProductType());
                mateProductReport.setMateCategory(mpTemplateModel.getMateCategory());
                mateProductReport.setMateProductName(mateProductMapper.selectMateProductName(resMap));
                mateProductReport.setMaterialId(mpTemplateModel.getMaterialId());
                mateProductReport.setAllNum(Long.valueOf(mpTemplateModel.getBuyNum()));
                mateProductReport.setEnterNum(mpTemplate.getEnterNum());
                mateProductReport.setPurchaseTime(mpTemplateModel.getExpectTime());
                checkReportMapper.insertReportProduct(mateProductReport);

                for (MateProductDevice mateProductDevice : mateProductDevices) {
                    MateProductCheckModel mateProductCheckModel = new MateProductCheckModel();
                    mateProductCheckModel.setMateCheckId(mateProductCheck.getMateCheckId());
                    mateProductCheckModel.setDeptId(deptId);
                    mateProductCheckModel.setCheckTool(mateProductDevice.getDeviceName());
                    mateProductCheckModel.setActuralResult(mateProductDevice.getCheckResult());
                    mateProductCheckModel.setCheckConclusion("合格");
                    mateProductCheckModel.setMateCheckId(mateProductCheck.getMateCheckId());
                    mateProductCheckModel.setCheckProject(mateProductDevice.getCheckProject());
                    mateProductCheckModel.setScienceRequest(mateProductDevice.getCheckCriterion());
                    checkReportMapper.insertCheckReportModel(mateProductCheckModel);
                }
*/
            }

            MateImport mateImport = new MateImport();
            mateImport.setDeptId(deptId);
            mateImport.setImportOrderNum(mpTemplate.getImportOrderNum());
            mateImport.setRemark(mpTemplate.getImportRemark());
            mateImport.setImportStatus("1");
            mateImportMapper.insertImport(mateImport);
            MateImportModel mateImportModel = new MateImportModel();
            for (MpTemplateModel mpTemplateModel : mpTemplateModels) {
                Map<String, Object> importMap = new HashMap<>();
                importMap.put("deptId", deptId);
                importMap.put("mateProductId", mpTemplateModel.getMateproductId());

                mateImportModel.setImportId(mateImport.getImportId());
                mateImportModel.setDeptId(deptId);
                mateImportModel.setMateProductType(mpTemplateModel.getMateProductType());
                mateImportModel.setMateProductName(mateProductMapper.selectMateProductName(importMap));
                mateImportModel.setImportNum(mpTemplateModel.getBuyNum());
                mateImportModel.setMateBatch(mpTemplateModel.getMateBatch());
                mateImportModel.setMateproductId(mpTemplateModel.getMateproductId());
                mateImportModel.setReportOrderNum(reportOrderNum);
                mateImportModel.setAcceptId(mpTemplateModel.getAcceptId());
                mateImportModel.setAcceptName(mpTemplateModel.getAcceptName());
                mateImportModel.setWarehousingId(mpTemplateModel.getWarehousingId());
                mateImportModel.setWarehousingName(mpTemplateModel.getWarehousingName());
                mateImportModel.setEnterNum(mpTemplateModel.getEnterNum());
                mateImportModel.setImportDate(mpTemplateModel.getImportDate());
                mateImportModel.setMaterialName(mateManufactuterMapper.selectMaterialName(mpTemplateModel.getMaterialId()));
                mateImportModel.setMaterialId(mpTemplateModel.getMaterialId());
                mateImportMapper.insertImportModel(mateImportModel);
                MateWarehousing mateWarehousing = new MateWarehousing();
                String mateproductType = mpTemplateModel.getMateProductType();
                String enterNum = mpTemplateModel.getEnterNum();
                int nums = Integer.valueOf(mpTemplateModel.getBuyNum());
                mateWarehousing.setDeptId(deptId);
                mateWarehousing.setEnterNum(enterNum);
                mateWarehousing.setMateProductType(mateproductType);
                mateWarehousing.setMateproductId(mpTemplateModel.getMateproductId());
                mateWarehousing.setWarehousingNum(Integer.valueOf(mpTemplateModel.getBuyNum()));
                mateWarehousing.setMaterialId(mpTemplateModel.getMaterialId());
                mateWarehousing.setMateBatch(mpTemplateModel.getMateBatch());
                MateWarehousing mateWarehousing1 = mateWarehousingMapper.selectMateWarehousing(mateWarehousing);
                // 保存入库记录
                if (mateWarehousing1 == null) {
                    mateWarehousingMapper.insertMateWarehousing(mateWarehousing);
                    // Long warehousingId = mateWarehousingMapper.insertMateWarehousing(mateWarehousing);
                    // mateWarehousing1 = new MateWarehousing();
                    // mateWarehousing1.setWarehousingId(warehousingId);
                } else {
                    int warehousingNum = mateWarehousing1.getWarehousingNum();
                    int warehousingNum1 = warehousingNum + nums;
                    if (warehousingNum1 < 0) {
                        new AjaxResult(AjaxResult.Type.ERROR, "库存不足");
                        throw new RuntimeException("库存不足");
                    }
                    mateWarehousing.setWarehousingNum(warehousingNum1);
                    mateWarehousing.setWarehousingId(mateWarehousing1.getWarehousingId());
                    mateWarehousingMapper.updateMateWarehousing(mateWarehousing);
                }
                // 保存入库操作记录
                WarehousingRecords wr = new WarehousingRecords();
                wr.setWarehousingId(mateWarehousing.getWarehousingId()); // 库存ID
                wr.setSupplierId(mpTemplateModel.getMaterialId()); // 供应商ID
                wr.setInventoryNum(Integer.parseInt(mpTemplateModel.getBuyNum())); // 操作数量
                wr.setInventoryStatus("0"); // 操作类型 ： 入库
                wr.setCreateTime(new SimpleDateFormat("yyyy-MM-dd").parse(mpTemplateModel.getImportDate())); // 操作时间
                wr.setCreateBy(SecurityUtils.getLoginUser().getNickName()); // 操作人
                wr.setAllnum(Integer.parseInt(mpTemplateModel.getBuyNum())); // 操作数量
                wr.setOrderNum("YCG-" + new SimpleDateFormat("yyyyMMddHHmm").format(new Date())); // 采购订单号
                warehousingRecordsMapper.insertWarehousingRecords(wr);
            }
            num = 1;
        } catch (Exception e) {
            num = 0;
            throw new RuntimeException(e.getMessage());
        }
        return num;
    }

    @Override
    public Map<String, Object> selectLastRecord(Long deptId) {
        Map<String, Object> resMap = new HashMap<>();

        MpTemplate mpTemplate = new MpTemplate();

        MateInventory mateInventory = mateInventoryMapper.selectMateInventoryLast(deptId);

        MateInventoryPlan mateInventoryPlan = mateInventoryMapper.selectPlanLast(deptId);

        MateEvaluate mateEvaluate = mateManufactuterMapper.selectEvaluateLast(deptId);

        MateQualified mateQualified = mateQualifiedMapper.selectLast(deptId);

        MateProductDetail mateProductDetail = detailMapper.selectLast(deptId);

        MateCheck mateCheck = mateCheckMapper.selectLast(deptId);

        MateImport mateImport = mateImportMapper.selectLast(deptId);

        if (mateInventory != null && mateEvaluate != null) {
            if (mateInventory.getInventoryNum() != null) {
                String input = mateInventory.getInventoryNum();
                // String numberStr = input.replaceAll("[^0-9]", "");
                // int number = Integer.parseInt(numberStr);
                // number++;
                // String newNumber = String.format("%03d", number);
                // String newString = input.replaceAll("[0-9]+", newNumber);
                String newString = "";
                Matcher m = Pattern.compile("(\\d+)(?!.*\\d)").matcher(input);
                if (m.find()) newString = input.replaceFirst("(\\d+)(?!.*\\d)",String.format("%03d", Integer.parseInt(m.group()) + 1));
                mpTemplate.setInventoryNum(newString);
            }

            if (mateInventoryPlan.getInventoryPlanNum() != null) {
                String input = mateInventoryPlan.getInventoryPlanNum();
                // String numberStr = input.replaceAll("[^0-9]", "");
                // int number = Integer.parseInt(numberStr);
                // number++;
                // String newNumber = String.format("%03d", number);
                // String newString2 = input.replaceAll("[0-9]+", newNumber);
                String newString2 = "";
                Matcher m = Pattern.compile("(\\d+)(?!.*\\d)").matcher(input);
                if (m.find()) newString2 = input.replaceFirst("(\\d+)(?!.*\\d)",String.format("%03d", Integer.parseInt(m.group()) + 1));
                mpTemplate.setInventoryPlanNum(newString2);
            }

            if (mateEvaluate.getEvaluateOrderNum() != null) {
                String input = mateEvaluate.getEvaluateOrderNum();
                // String numberStr = input.replaceAll("[^0-9]", "");
                // int number = Integer.parseInt(numberStr);
                // number++;
                // String newNumber = String.format("%03d", number);
                // String newString = input.replaceAll("[0-9]+", newNumber);
                String newString = "";
                Matcher m = Pattern.compile("(\\d+)(?!.*\\d)").matcher(input);
                if (m.find()) newString = input.replaceFirst("(\\d+)(?!.*\\d)",String.format("%03d", Integer.parseInt(m.group()) + 1));
                mpTemplate.setEvaluateOrderNum(newString);
            }

            if (mateQualified.getQualifiedOrderNum() != null) {
                String input = mateQualified.getQualifiedOrderNum();
                // String numberStr = input.replaceAll("[^0-9]", "");
                // int number = Integer.parseInt(numberStr.equals("") ? "0":numberStr);
                // number++;
                // String newNumber = String.format("%03d", number);
                // String newString = input.replaceAll("[0-9]+", newNumber);
                String newString = "";
                Matcher m = Pattern.compile("(\\d+)(?!.*\\d)").matcher(input);
                if (m.find()) newString = input.replaceFirst("(\\d+)(?!.*\\d)",String.format("%03d", Integer.parseInt(m.group()) + 1));
                mpTemplate.setQualifiedOrderNum(newString);
            }

            if (mateProductDetail.getDetailOrderNum() != null) {
                String input = mateProductDetail.getDetailOrderNum();
                // String numberStr = input.replaceAll("[^0-9]", "");
                // int number = Integer.parseInt(numberStr.equals("") ? "0":numberStr);
                // number++;
                // String newNumber = String.format("%03d", number);
                // String newString = input.replaceAll("[0-9]", newNumber);

                String newString = "";
                Matcher m = Pattern.compile("(\\d+)(?!.*\\d)").matcher(input);
                if (m.find()) newString = input.replaceFirst("(\\d+)(?!.*\\d)",String.format("%03d", Integer.parseInt(m.group()) + 1));
                mpTemplate.setDetailOrderNum(newString);
            }

            if (mateCheck.getCheckOrdernum() != null) {
                String input = mateCheck.getCheckOrdernum();
                // String numberStr = input.replaceAll("[^0-9]", "");
                // int number = Integer.parseInt(numberStr);
                // number++;
                // String newNumber = String.format("%03d", number);
                // String newString = input.replaceAll("[0-9]+", newNumber);
                String newString = "";
                Matcher m = Pattern.compile("(\\d+)(?!.*\\d)").matcher(input);
                if (m.find()) newString = input.replaceFirst("(\\d+)(?!.*\\d)",String.format("%03d", Integer.parseInt(m.group()) + 1));
                mpTemplate.setCheckOrderNum(newString);
            }

            if (mateImport.getImportOrderNum() != null) {
                String input = mateImport.getImportOrderNum();
                // String numberStr = input.replaceAll("[^0-9]", "");
                // int number = Integer.parseInt(numberStr);
                // number++;
                // String newNumber = String.format("%03d", number);
                // String newString = input.replaceAll("[0-9]+", newNumber);
                String newString = "";
                Matcher m = Pattern.compile("(\\d+)(?!.*\\d)").matcher(input);
                if (m.find()) newString = input.replaceFirst("(\\d+)(?!.*\\d)",String.format("%03d", Integer.parseInt(m.group()) + 1));
                mpTemplate.setImportOrderNum(newString);
            }

            mpTemplate.setAuditId(mateInventory.getAuditId());
            mpTemplate.setAuditName(mateInventory.getAuditName());
            mpTemplate.setApplyId(mateEvaluate.getApplyId());
            mpTemplate.setApplyName(mateEvaluate.getApplyName());
            mpTemplate.setQualifiedBzId(mateQualified.getBzId());
            mpTemplate.setQualifiedBzName(mateQualified.getBzName());
            mpTemplate.setQualifiedPzId(mateQualified.getPzId());
            mpTemplate.setQualifiedPzName(mateQualified.getPzName());
            mpTemplate.setCheckAuditId(mateCheck.getAuditId());
            mpTemplate.setCheckAuditName(mateCheck.getAuditName());
            mpTemplate.setCheckAuditTime(mateCheck.getAuditTime());
            mpTemplate.setCheckNum(String.valueOf(mateCheck.getNum()));
            mpTemplate.setZjId(mateCheck.getZjId());
            mpTemplate.setZjName(mateCheck.getZjName());

            resMap.put("mpTemplate", mpTemplate);
        } else {
            resMap.put("mpTemplate", mpTemplate);
        }
        return resMap;
    }
}
